﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace YN.Utils.Web
{
    public class PageBase : Page
    {
        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);
        //    
        //    AddMetaContentType();
        //}

        private void AddMetaContentType()
        {
            if (Page.Header == null)
                return;

            HtmlMeta meta = new HtmlMeta();
            meta.HttpEquiv = "Content-type";
            meta.Content = String.Format("{0}; charset={1}", Response.ContentType, Response.ContentEncoding.HeaderName);

            Page.Header.Controls.Add(meta);
        }


        public void SetPageTitle(string title)
        {
            if (Page.Header == null)
                return;

            if (!string.IsNullOrEmpty(title))
                Page.Header.Title = title;
        }

        public string GetAppSetting(string key)
        {
            return ConfigHelper.GetAppSetting(key);
        }

        public string GetAppSetting(int index)
        {
            return ConfigHelper.GetAppSetting(index);
        }

        public string GetStringUrlParam(string key)
        {
            return UrlHelper.GetStringUrlParam(key);
        }
        public string GetStringUrlParam(string key, string defaultValue)
        {
            return UrlHelper.GetStringUrlParam(key, defaultValue);
        }

        public int GetIntUrlParam(string key)
        {
            return UrlHelper.GetIntUrlParam(key);
        }
        public int GetIntUrlParam(string key, int defaultValue)
        {
            return UrlHelper.GetIntUrlParam(key, defaultValue);
        }

        public static bool GetBoolUrlParam(string key)
        {
            return UrlHelper.GetBoolUrlParam(key);
        }
        public static bool GetBoolUrlParam(string key, bool defaultValue)
        {
            return UrlHelper.GetBoolUrlParam(key, defaultValue);
        }

        public static DateTime GetDateTimeUrlParam(string key)
        {
            return UrlHelper.GetDateTimeUrlParam(key);
        }
        public static DateTime GetDateTimeUrlParam(string key, DateTime defaultValue)
        {
            return UrlHelper.GetDateTimeUrlParam(key, defaultValue);
        }

        public void Alert(string message)
        {
            MsgBoxHelper.Alert(message);
        }

        public void Confirm(string message, string url)
        {
            MsgBoxHelper.Confirm(message, url);
        }

        public void Location(string url)
        {
            MsgBoxHelper.Location(url);
        }

        public void Location(string message, string url)
        {
            MsgBoxHelper.Location(message, url);
        }

        public void Close()
        {
            MsgBoxHelper.Close();
        }

        public void Close(string message)
        {
            MsgBoxHelper.Close(message);
        }

        public void GoBack()
        {
            MsgBoxHelper.GoBack();
        }

        public void GoBack(string message)
        {
            MsgBoxHelper.GoBack(message);
        }

        public string CutLen(object obj, int length)
        {
            if (obj == null)
                return string.Empty;

            return StringHelper.CutLen(obj.ToString(), length);
        }

        public void SaveCookie(string cookieName, string[] paramNames, string[] paramValues, DateTime expires)
        {
            CookieHelper.SaveCookie(cookieName, paramNames, paramValues, expires);
        }

        public void SaveCookie(string cookieName, string paramName, string paramValue, DateTime expires)
        {
            CookieHelper.SaveCookie(cookieName, paramName, paramValue, expires);
        }

        public void ClearCookie(string cookieName)
        {
            CookieHelper.ClearCookie(cookieName);
        }

        public int GetIntFormParam(string key)
        {
            return FormHelper.GetIntFormParam(key);
        }
        public int GetIntFormParam(string key, int def)
        {
            return FormHelper.GetIntFormParam(key, def);
        }

        public string GetStringFormParam(string key)
        {
            return FormHelper.GetStringFormParam(key);
        }
        public string GetStringFormParam(string key, string def)
        {
            return FormHelper.GetStringFormParam(key, def);
        }

        public static bool GetBoolFormParam(string key)
        {
            return FormHelper.GetBoolFormParam(key);
        }
        public static bool GetBoolFormParam(string key, bool defaultValue)
        {
            return FormHelper.GetBoolFormParam(key, defaultValue);
        }

        public DateTime GetDateTimeFormParam(string key)
        {
            return FormHelper.GetDateTimeFormParam(key);
        }
        public DateTime GetDateTimeFormParam(string key, DateTime def)
        {
            return FormHelper.GetDateTimeFormParam(key, def);
        }


        private void AddMeta(string name, string content)
        {
            if (Page.Header == null)
                return;
            
            HtmlMeta meta = new HtmlMeta();
            meta.Name = name;
            meta.Content = content;

            Page.Header.Controls.Add(meta);
        }

        public void AddRobots(string robots)
        {
            AddMeta("robots", robots);
        }

        public void AddDescription(string description)
        {
            AddMeta("description", description);
        }

        public void AddAuthor(string author)
        {
            AddMeta("author", author);
        }

        public void AddKeyword(string keyword)
        {
            AddMeta("keywords", keyword);
        }

        public void AddStyleSheetFile(string path)
        {
            if (Page.Header == null)
                return;

            LiteralControl li = new LiteralControl();
            li.Text = string.Format("\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", path);

            Page.Header.Controls.Add(li);
        }

        public void AddIcon(string path)
        {
            if (Page.Header == null)
                return;

            LiteralControl li1 = new LiteralControl();
            LiteralControl li2 = new LiteralControl();

            li1.Text = string.Format("\r\n<link href=\"upfiles/files/siteico.ico\" rel=\"icon\" type=\"image/x-icon\" />", path);
            li2.Text = string.Format("\r\n<link href=\"upfiles/files/siteico.ico\" rel=\"shortcut icon\" type=\"image/x-icon\" />", path);
            
            Page.Header.Controls.Add(li1);
            Page.Header.Controls.Add(li2);
        }

        public void AddJavaScriptFile(string path)
        {
            if (Page.Header == null)
                return;

            LiteralControl li = new LiteralControl();
            li.Text = string.Format("\r\n<script src=\"{0}\" type=\"text/javascript\"></script>", path);

            Page.Header.Controls.Add(li);
        }
        
    }
}
